import {
    nextAll,
    httpProcess
} from '../utils/queue.js'
import store from '../store/index.js'
import {
    staticToken,
    superToken
} from '@/api/token.js'
import 
    request from '@/http/request.js'
/**
 * @auther zhonghuanting
 * @fun intercept 拦截公共方法 requestBefore请求前处理  rquestTime请求中处理 requestAfter请求后处理  next执行开关
 */
export function intercept(config) {
	return new Promise((resolve, reject) => {
		httpProcess(requestBefore, rquestTime, requestAfter, config).then(res => {
			if (res) {
				resolve(res)
			}
		})
	})
}

export function rquestTime(config, next) {
    var result = request(config)
    next(result)
}
export async function requestBefore(config, next) {
    if (!store.getters.token.JWT) {
        var res = await staticToken()
        if (res.code) {
            store.dispatch("token", res.data);
        } else {
            return;
        }
    }
    if (!store.getters.token.JWT||config.jwt) {
        var res = await superToken()
        if (res.code) {
            store.dispatch("token", res.data);
        } else {
            return;
        }
    }
    next()
}
export function requestAfter(response, next) {
    return response;
    next()
}